/*DONE* Table [dbo].[<CompanyName>$LSC Member Contact$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 29.7.2020 - JI ******/
/*Add prestaging table for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT MAX([Company]) FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$LSC Member Contact$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$LSC Member Contact$5ecfc871-5d82-43f1-9c54-59685e82318d](
[timestamp] [timestamp] NOT NULL,
	[Account No_] [nvarchar](20) NOT NULL,
	[Contact No_] [nvarchar](20) NOT NULL,
	[Club Code] [nvarchar](10) NOT NULL,
	[Scheme Code] [nvarchar](10) NOT NULL,
	[Main Contact] [tinyint] NOT NULL,
	[Name] [nvarchar](100) NOT NULL,
	[Search Name] [nvarchar](100) NOT NULL,
	[Name 2] [nvarchar](50) NOT NULL,
	[Address] [nvarchar](100) NOT NULL,
	[Address 2] [nvarchar](50) NOT NULL,
	[City] [nvarchar](30) NOT NULL,
	[House_Apartment No_] [nvarchar](30) NOT NULL,
	[Post Code] [nvarchar](20) NOT NULL,
	[E-Mail] [nvarchar](80) NOT NULL,
	[Home Page] [nvarchar](80) NOT NULL,
	[Phone No_] [nvarchar](30) NOT NULL,
	[Mobile Phone No_] [nvarchar](30) NOT NULL,
	[Territory Code] [nvarchar](10) NOT NULL,
	[Picture] [image] NULL,
	[County] [nvarchar](30) NOT NULL,
	[Country_Region Code] [nvarchar](10) NOT NULL,
	[Language Code] [nvarchar](10) NOT NULL,
	[Gender] [int] NOT NULL,
	[Date of Birth] [datetime] NOT NULL,
	[Birthday] [nvarchar](4) NOT NULL,
	[Marital Status] [int] NOT NULL,
	[Expiration Period Type] [int] NOT NULL,
	[Blocked] [tinyint] NOT NULL,
	[Reason Blocked] [nvarchar](10) NOT NULL,
	[Date Blocked] [datetime] NOT NULL,
	[Blocked by] [nvarchar](50) NOT NULL,
	[Created Date] [datetime] NOT NULL,
	[Created by] [nvarchar](50) NOT NULL,
	[Image] [uniqueidentifier] NOT NULL,
	[Privacy Blocked] [tinyint] NOT NULL,
	[No_ Series] [nvarchar](20) NOT NULL,
	[External ID] [nvarchar](50) NOT NULL,
	[External System] [nvarchar](50) NOT NULL,
	[First Name] [nvarchar](30) NOT NULL,
	[Middle Name] [nvarchar](30) NOT NULL,
	[Surname] [nvarchar](30) NOT NULL,
	[Salutation Code] [nvarchar](10) NOT NULL,
	[Search E-Mail] [nvarchar](80) NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[Member Card Temporary] [nvarchar](100) NOT NULL,
	[$systemCreatedAt] [datetime] NOT NULL,
	[$systemCreatedBy] [uniqueidentifier] NOT NULL,
	[$systemModifiedAt] [datetime] NOT NULL,
	[$systemModifiedBy] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$LSC Member Contact$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[Account No_] ASC,
	[Contact No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$LSC Member Contact$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i+1
END